const os = require( 'os' );
const fs = require( 'fs' );
const path = require( 'path' );
const moment = require( 'moment' );
const express = require( 'express' );

const app = express();
const webRootpath = path.join( __dirname );

let now = moment().format( 'YYYY-MM-DD' )
console.log( now );
const filepath = path.join( __dirname, 'logs', now + '.log' );
// let pathname;
app.use(function ( req, res ) { 
   
    let tt = moment().format( 'YYYY-MM-DD hh:mm:ss' );
    let ip = req.socket.remoteAddress;
    let userAgent = req.headers[ 'user-agent' ];
    let data = `${ tt }-${ ip }-${ userAgent }${ os.EOL }`;
    
    let pathname = req.url;
    let webfilepath = path.join( webRootpath, pathname );
    let extname = path.extname( webfilepath );
    if ( extname === '.html' ) {
        fs.appendFile( filepath, data, err => { } );
    } else { 
        return;
    }
    res.send('hello')
} )
app.listen( 8080 );